@using BlazorApp.Pages.Cluster
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Service.k8s
@using BlazorApp.Utils
@using BlazorApp.Utils.Prometheus.Models.Interfaces
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase
@if (CadvisorMetrics is { Count: > 0 })
{
    <Button OnClick="@(() => OnNodeMetricsClick(Node))" Type="@ButtonType.Link">@L["Detail"]</Button>

    <HTable>
        <HTr>
            <HTd>
                @L["CPU"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["machine_cpu_sockets"]
                                            MetricName="machine_cpu_sockets">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["machine_cpu_physical_cores"]
                                            MetricName="machine_cpu_physical_cores">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["machine_cpu_cores"]
                                            MetricName="machine_cpu_cores">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Memory"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["machine_memory_bytes"]
                                            MetricName="machine_memory_bytes" Unit="bytes">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Disk"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_blkio_device_usage_total"]
                                            MetricName="container_blkio_device_usage_total"
                                            Unit="bytes">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_file_descriptors"]
                                            MetricName="container_file_descriptors">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_fs_inodes_free"]
                                            MetricName="container_fs_inodes_free">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Process"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@Metrics" Title=@L["process_open_fds"]
                                            MetricName="process_open_fds">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@Metrics" Title=@L["process_max_fds"]
                                            MetricName="process_max_fds">
                </MetricsCounterGaugeSumView>

                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_ulimits_soft"]
                                            MetricName="container_ulimits_soft">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Network"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_network_receive_bytes_total"]
                                            MetricName="container_network_receive_bytes_total"
                                            Unit="bytes">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics" Title=@L["container_network_receive_errors_total"]
                                            MetricName="container_network_receive_errors_total">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics"
                                            Title=@L["container_network_receive_packets_dropped_total"]
                                            MetricName="container_network_receive_packets_dropped_total">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@CadvisorMetrics"
                                            Title=@L["container_network_receive_packets_total"]
                                            MetricName="container_network_receive_packets_total">
                </MetricsCounterGaugeSumView>

            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Pod"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@ResourceMetrics" Title=@L["node_memory_working_set_bytes"]
                                            MetricName="node_memory_working_set_bytes" Unit="bytes">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@ResourceMetrics" Title=@L["pod_memory_working_set_bytes"]
                                            MetricName="pod_memory_working_set_bytes" Unit="bytes">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@ResourceMetrics" Title=@L["container_memory_working_set_bytes"]
                                            MetricName="container_memory_working_set_bytes" Unit="bytes">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>
        <HTr>
            <HTd>
                @L["Kubelet"]
            </HTd>
            <HTd>
                <MetricsCounterGaugeSumView AllMetrics="@Metrics" Title=@L["kubelet_restarted_pods_total"]
                                            MetricName="kubelet_restarted_pods_total">
                </MetricsCounterGaugeSumView>

                <MetricsCounterGaugeSumView AllMetrics="@Metrics" Title=@L["kubelet_running_pods"]
                                            MetricName="kubelet_running_pods">
                </MetricsCounterGaugeSumView>
                <MetricsCounterGaugeSumView AllMetrics="@Metrics" Title=@L["kubelet_started_pods_errors_total"]
                                            MetricName="kubelet_started_pods_errors_total">
                </MetricsCounterGaugeSumView>
            </HTd>
        </HTr>

    </HTable>
}


@code {
    [Parameter] public V1Node Node { get; set; }

    [Inject] INodeService NodeService { get; set; }

    private IList<IMetric> Metrics { get; set; }
    private IList<IMetric> CadvisorMetrics { get; set; }
    private IList<IMetric> ResourceMetrics { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Metrics = await NodeService.GetMetrics(Node.Name());
        CadvisorMetrics = await NodeService.GetCadvisorMetrics(Node.Name());
        ResourceMetrics = await NodeService.GetResourceMetrics(Node.Name());
        await base.OnInitializedAsync();
    }

    private async Task OnNodeMetricsClick(V1Node item)
    {
        await PageDrawerHelper<V1Node>.Instance
            .SetDrawerService(PageDrawerService.DrawerService)
            .ShowDrawerAsync<NodeMetricsDetailTabView, V1Node, bool>(item);
    }

}
